package com.taptech.doufu.ui.fragment;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.taptech.doufu.constant.EventKey;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.constant.SPKey;
import com.taptech.doufu.event.BaseEventBusBean;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.YouthModelActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.CommonViewPagerAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.HomePageIndex;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.event.EventBusHomePageSwitch;
import com.taptech.doufu.listener.IListScollerState;
import com.taptech.doufu.manager.UserLogManager;
import com.taptech.doufu.bean.ConfigBean;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.ui.activity.HomeSearchActivity;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.tablayout.TabLayout;
import com.taptech.doufu.ui.view.colorful.Colorful;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 小说页面
 */
public class DFHomeFragment extends DiaobaoBaseFragment implements HttpResponseListener,IListScollerState {

    private static final String SP_KEY_HAS_CHANGE_PAGE = "HAS_CHANGE_PAGE";
    private static final String SP_KEY_STAY_PAGE = "SP_KEY_STAY_PAGE";

    private TTHomeViewPager vpContent;
    private List<Fragment> mSweepListViews;
    private int currentPage = 0;
    private DFBLNovelFragment dfNovelFragment;
    private DFBLNovelFragment dfNovelFinishedFragment;
    private DFBLNovelFragment dfNovelCopyrightFragment;
    private DFBLNovelFragment dfNovelFragment2;
    private LinearLayout llSearch;
    private CartoonRecommendFragment cartoonRecommendFragment;
    private DFChoiceFragment choiceFragment;
    private View rootView;
    private TabLayout tlTop;
    private RelativeLayout rlBuoy;
    private ImageView ivBuoy,ivBuoyClose;
    private View layTitleBar;
    private View layTitleBarBg;
    private ImageView ivSearch;
    private View btnQuitYouthMode;

    private boolean isNightBg = false;
    private boolean isAnalysisLock = false;


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onHomePageSwitch(EventBusHomePageSwitch event) {
        if (event != null && EventBusHomePageSwitch.tag.equals(event.getTag())) {
            if (vpContent != null) {
                vpContent.setCurrentItem(event.getIndex());
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void openYouthModel(BaseEventBusBean event){
        if (event != null && EventKey.YOUTH_MODEL.equals(event.getTag())) {
            setYouthModel();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(rootView == null){
            EventBus.getDefault().register(this);
            isNightBg = WeMediaApplication.getInstance().isDayNightMode_Night;
            rootView = inflater.inflate(R.layout.fragment_dfhome, null);
            ivSearch = rootView.findViewById(R.id.ivSearch);
            btnQuitYouthMode = rootView.findViewById(R.id.btnQuitYouthMode);
            btnQuitYouthMode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    YouthModelActivity.Companion.startActivity(getActivity());
                }
            });
            layTitleBar = rootView.findViewById(R.id.layTitleBar);
            layTitleBarBg = rootView.findViewById(R.id.layTitleBarBg);

            if (Build.VERSION.SDK_INT >= 19) {
                //设置状态栏高度
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, ScreenUtil.dip2px(getActivity(),44)+ScreenUtil.getStatusHeight(getActivity()));
                layTitleBar.setLayoutParams(params);
                layTitleBarBg.setLayoutParams(params);
                layTitleBar.setPadding(0,ScreenUtil.getStatusHeight(getActivity()),0,0);
                layTitleBarBg.setPadding(0,ScreenUtil.getStatusHeight(getActivity()),0,0);
            }

            rlBuoy =  rootView.findViewById(R.id.rlBuoy);
            ivBuoy = (ImageView) rootView.findViewById(R.id.ivBuoy);
            ivBuoyClose = (ImageView) rootView.findViewById(R.id.ivBuoyClose);
            tlTop = (TabLayout) rootView.findViewById(R.id.tlTop);
            tlTop.setTabMode(TabLayout.MODE_SCROLLABLE);
            vpContent = (TTHomeViewPager) rootView.findViewById(R.id.vp_home_content);
            vpContent.setOffscreenPageLimit(6);
            llSearch = (LinearLayout) rootView.findViewById(R.id.llSearch);
            llSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TMAnalysis.event(v.getContext(), "home2-search");
                    UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_SEARCH);
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), HomeSearchActivity.class);
                    startActivity(intent);
                }
            });
            setYouthModel();
            setCurrentFragment();
        }

        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setupColorful();
    }


    private void setYouthModel() {
        String pwd = DiaoBaoSharedPreferences.getSharedPreferencesValueToString(SPKey.YOUTH_MODEL_PWD, getActivity(), "");
        if (pwd != null && pwd.length() > 0) {
            isTop = true;
            btnQuitYouthMode.setVisibility(View.VISIBLE);
            llSearch.setVisibility(View.GONE);
            initYouthModeFragments();
            setTitleBar(400,currentPage);
        }else{
            btnQuitYouthMode.setVisibility(View.GONE);
            llSearch.setVisibility(View.VISIBLE);
            initsFragments();
            setTitleBar(jingxuanY,currentPage);
        }
    }

    private void setCurrentFragment() {
        currentPage = DiaoBaoSharedPreferences.getSharedPreferencesValueToInt(SP_KEY_STAY_PAGE,WeMediaApplication.applicationContext,0);
        isAnalysisLock = true;
        vpContent.setCurrentItem(currentPage, false);
    }




    public void setCurrentFragment(int page) {
        boolean has_change_page = DiaoBaoSharedPreferences.getSharedPreferencesValueToBoolean(SP_KEY_HAS_CHANGE_PAGE, WeMediaApplication.applicationContext, false);
        if (!has_change_page){
            currentPage = page;
            isAnalysisLock = true;
            vpContent.setCurrentItem(currentPage, false);
        }
    }

    public void scrollToTop() {
        switch (currentPage){
            case HomePageIndex.PAGE_INDEX_JINGXUAN:
                if (choiceFragment != null) {
                    choiceFragment.scrollToTop();
                }
                break;
            case HomePageIndex.PAGE_INDEX_RAINBOW:
                if (dfNovelFragment != null) {
                    dfNovelFragment.scrollToTop();
                }
                break;
            case HomePageIndex.PAGE_INDEX_RAINBOW_FINISHED:
                if (dfNovelFinishedFragment != null) {
                    dfNovelFinishedFragment.scrollToTop();
                }
                break;
            case HomePageIndex.PAGE_INDEX_COPYRIGHT:
                if (dfNovelCopyrightFragment != null) {
                    dfNovelCopyrightFragment.scrollToTop();
                }
                break;
            case HomePageIndex.PAGE_INDEX_ROMANCE:
                if (dfNovelFragment2 != null) {
                    dfNovelFragment2.scrollToTop();
                }
                break;
            case HomePageIndex.PAGE_INDEX_COMIC:
                if (cartoonRecommendFragment != null) {
                    cartoonRecommendFragment.scrollToTop();
                }
                break;
        }
    }


    public void startSpeaker() {
        switch (currentPage){
            case HomePageIndex.PAGE_INDEX_JINGXUAN:
                if (choiceFragment != null) { choiceFragment.startSpeaker(); }
                if (dfNovelFragment != null) { dfNovelFragment.stopSpeaker(); }
                if (dfNovelFinishedFragment != null) { dfNovelFinishedFragment.stopSpeaker(); }
                if (dfNovelFragment2 != null) { dfNovelFragment2.stopSpeaker(); }
                if (cartoonRecommendFragment != null) { cartoonRecommendFragment.stopSpeaker(); }
                if (dfNovelCopyrightFragment != null) { dfNovelCopyrightFragment.stopSpeaker(); }
                break;
            case HomePageIndex.PAGE_INDEX_RAINBOW:
                if (choiceFragment != null) { choiceFragment.stopSpeaker(); }
                if (dfNovelFragment != null) { dfNovelFragment.startSpeaker(); }
                if (dfNovelFinishedFragment != null) { dfNovelFinishedFragment.stopSpeaker(); }
                if (dfNovelFragment2 != null) { dfNovelFragment2.stopSpeaker(); }
                if (cartoonRecommendFragment != null) { cartoonRecommendFragment.stopSpeaker(); }
                if (dfNovelCopyrightFragment != null) { dfNovelCopyrightFragment.stopSpeaker(); }
                break;

            case HomePageIndex.PAGE_INDEX_RAINBOW_FINISHED:
                if (choiceFragment != null) { choiceFragment.stopSpeaker(); }
                if (dfNovelFragment != null) { dfNovelFragment.stopSpeaker(); }
                if (dfNovelFinishedFragment != null) { dfNovelFinishedFragment.startSpeaker(); }
                if (dfNovelFragment2 != null) { dfNovelFragment2.stopSpeaker(); }
                if (cartoonRecommendFragment != null) { cartoonRecommendFragment.stopSpeaker(); }
                if (dfNovelCopyrightFragment != null) { dfNovelCopyrightFragment.stopSpeaker(); }
                break;
            case HomePageIndex.PAGE_INDEX_ROMANCE:
                if (choiceFragment != null) { choiceFragment.stopSpeaker(); }
                if (dfNovelFragment != null) { dfNovelFragment.stopSpeaker(); }
                if (dfNovelFinishedFragment != null) { dfNovelFinishedFragment.stopSpeaker(); }
                if (dfNovelFragment2 != null) { dfNovelFragment2.startSpeaker(); }
                if (cartoonRecommendFragment != null) { cartoonRecommendFragment.stopSpeaker(); }
                if (dfNovelCopyrightFragment != null) { dfNovelCopyrightFragment.stopSpeaker(); }

                break;
            case HomePageIndex.PAGE_INDEX_COMIC:
                if (choiceFragment != null) { choiceFragment.stopSpeaker(); }
                if (dfNovelFragment != null) { dfNovelFragment.stopSpeaker(); }
                if (dfNovelFinishedFragment != null) { dfNovelFinishedFragment.stopSpeaker(); }
                if (dfNovelFragment2 != null) { dfNovelFragment2.stopSpeaker(); }
                if (cartoonRecommendFragment != null) { cartoonRecommendFragment.startSpeaker(); }
                if (dfNovelCopyrightFragment != null) { dfNovelCopyrightFragment.stopSpeaker(); }
                break;

            case HomePageIndex.PAGE_INDEX_COPYRIGHT:
                if (choiceFragment != null) { choiceFragment.stopSpeaker(); }
                if (dfNovelFragment != null) { dfNovelFragment.stopSpeaker(); }
                if (dfNovelFinishedFragment != null) { dfNovelFinishedFragment.stopSpeaker(); }
                if (dfNovelFragment2 != null) { dfNovelFragment2.stopSpeaker(); }
                if (cartoonRecommendFragment != null) { cartoonRecommendFragment.stopSpeaker(); }
                if (dfNovelCopyrightFragment != null) { dfNovelCopyrightFragment.startSpeaker(); }

                break;
        }
    }

    public void stopSpeaker() {
        if (choiceFragment != null) {
            choiceFragment.stopSpeaker();
        }
        if (dfNovelFragment != null) {
            dfNovelFragment.stopSpeaker();
        }

        if (dfNovelFinishedFragment != null) {
            dfNovelFinishedFragment.stopSpeaker();
        }

        if (dfNovelFragment2 != null) {
            dfNovelFragment2.stopSpeaker();
        }
        if (cartoonRecommendFragment != null) {
            cartoonRecommendFragment.stopSpeaker();
        }
        if (dfNovelCopyrightFragment != null){
            dfNovelCopyrightFragment.stopSpeaker();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        DiaoBaoSharedPreferences.setSharedPreferencesValueToInt(SP_KEY_STAY_PAGE,currentPage,WeMediaApplication.applicationContext);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void setBuoy(final ConfigBean.Buoy buoy) {
        if (buoy != null && buoy.isVisible()) {
            if (rlBuoy.getVisibility() == View.VISIBLE) {
                return;
            }
            rlBuoy.setVisibility(View.VISIBLE);
            if (buoy.isShow_close_image()) {
                ivBuoyClose.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(buoy.getClose_image())){
                    GlideUtil.displayImage(ivBuoyClose,buoy.getClose_image(),buoy.getClose_image_width(),buoy.getClose_image_height());
                }
                ivBuoyClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rlBuoy.setVisibility(View.GONE);
                    }
                });
            }else{
                ivBuoyClose.setVisibility(View.GONE);
            }
            ivBuoy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(buoy.getUrl())) {
                        if (buoy.getObject_type() == Constant.OBJECT_TYPE_WEEX) {
                            SimpleWeexActivity.startActivity(getActivity(),buoy.getUrl());
                        }else{
                            Intent intent = new Intent();
                            intent.setClass(getActivity(), BrowseActivity.class);
                            intent.putExtra(Constant.URL, buoy.getUrl());
                            startActivity(intent);
                        }
                    }
                }
            });
            if (!TextUtils.isEmpty(buoy.getImage())){
                GlideUtil.displayImage(ivBuoy,buoy.getImage(),buoy.getImage_width(),buoy.getImage_height());
            }

        }else{
            rlBuoy.setVisibility(View.GONE);
        }
    }

    private int jingxuanY;   //精选
    private int rainbowY;    //彩虹
    private int rainbowFinishedY;    //彩虹
    private int romanceY;    //言情
    private int comicY;      //漫画
    private int copyrightY;  //版权合作
    private boolean isTop;  //是否在顶部

    private void setTitleBar(int scrollY,int pos) {
        if (currentPage == pos) {
            if (scrollY < 300) {
                float alpha = 1f/300f * scrollY;
                layTitleBarBg.setAlpha(alpha);
                if (!isTop) {
                    if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                        tlTop.setTabTextColors(getResources().getColor(R.color.white),getResources().getColor(R.color.white));
                        tlTop.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));
                        ivSearch.setImageResource(R.drawable.icon_search_white);
                    } else {
                        tlTop.setTabTextColors(getResources().getColor(R.color.white),getResources().getColor(R.color.white));
                        tlTop.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));
                        ivSearch.setImageResource(R.drawable.icon_search_white);
                    }
                }
                isTop = true;
            }else{
                layTitleBarBg.setAlpha(1f);
                if (isTop) {
                    if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                        tlTop.setTabTextColors(getResources().getColor(R.color.text_color_9),getResources().getColor(R.color.text_color_9));
                        tlTop.setSelectedTabIndicatorColor(getResources().getColor(R.color.text_color_9));
                        ivSearch.setImageResource(R.drawable.icon_search_gray_dark);
                    } else {
                        tlTop.setTabTextColors(getResources().getColor(R.color.text_color_1),getResources().getColor(R.color.text_color_1));
                        tlTop.setSelectedTabIndicatorColor(getResources().getColor(R.color.text_color_1));
                        ivSearch.setImageResource(R.drawable.icon_search_gray);
                    }
                }
                isTop = false;
            }
        }
    }

    private void initsFragments() {

        mSweepListViews = new ArrayList<>();
        if (choiceFragment == null) {
            choiceFragment = new DFChoiceFragment();
            choiceFragment.setiListScollerState(this);
            choiceFragment.setOnScrollYListener(new PullToRefreshListView.OnScrollYListener() {
                @Override
                public void onScrollY(int scrolledY) {
                    jingxuanY = scrolledY;
                    String pwd = DiaoBaoSharedPreferences.getSharedPreferencesValueToString(SPKey.YOUTH_MODEL_PWD, getActivity(), "");
                    if (!(pwd != null && pwd.length() > 0)) {
                        setTitleBar(scrolledY,HomePageIndex.PAGE_INDEX_JINGXUAN);
                    }
                }
            });
        }
        choiceFragment.setViewPager(vpContent);
        mSweepListViews.add(choiceFragment);
//        dfNovelFragment = new DFBLNovelFragment();
//        dfNovelFragment.setiListScollerState(this);
//        dfNovelFragment.setOnScrollYListener(new PullToRefreshListView.OnScrollYListener() {
//            @Override
//            public void onScrollY(int scrollY) {
//                rainbowY = scrollY;
//                setTitleBar(scrollY,HomePageIndex.PAGE_INDEX_RAINBOW);
//            }
//        });
//        dfNovelFragment.setSexType(ApiClient.SEX_TYPE_BL);
//        dfNovelFragment.setPageType(HomePageType.RAINBOW);
//        dfNovelFragment.setViewPager(vpContent);
//        mSweepListViews.add(dfNovelFragment);



//        dfNovelFinishedFragment = new DFBLNovelFragment();
//        dfNovelFinishedFragment.setiListScollerState(this);
//        dfNovelFinishedFragment.setOnScrollYListener(new PullToRefreshListView.OnScrollYListener() {
//            @Override
//            public void onScrollY(int scrollY) {
//                rainbowFinishedY = scrollY;
//                setTitleBar(scrollY,HomePageIndex.PAGE_INDEX_RAINBOW_FINISHED);
//            }
//        });
//        dfNovelFinishedFragment.setSexType(ApiClient.SEX_TYPE_BL);
//        dfNovelFinishedFragment.setPageType(HomePageType.RAINBOW_FINISHED);
//        dfNovelFinishedFragment.setViewPager(vpContent);
//        mSweepListViews.add(dfNovelFinishedFragment);

//        dfNovelFragment2 = new DFBLNovelFragment();
//        dfNovelFragment2.setOnScrollYListener(new PullToRefreshListView.OnScrollYListener() {
//            @Override
//            public void onScrollY(int scrollY) {
//                romanceY = scrollY;
//                setTitleBar(scrollY,HomePageIndex.PAGE_INDEX_ROMANCE);
//            }
//        });
//        dfNovelFragment2.setiListScollerState(this);
//        dfNovelFragment2.setSexType(ApiClient.SEX_TYPE_UN_BL);
//        dfNovelFragment2.setViewPager(vpContent);
//        mSweepListViews.add(dfNovelFragment2);

//
//        dfNovelCopyrightFragment = new DFBLNovelFragment();
//        dfNovelCopyrightFragment.setiListScollerState(this);
//        dfNovelCopyrightFragment.setOnScrollYListener(new PullToRefreshListView.OnScrollYListener() {
//            @Override
//            public void onScrollY(int scrollY) {
//                copyrightY = scrollY;
//                setTitleBar(scrollY,HomePageIndex.PAGE_INDEX_COPYRIGHT);
//            }
//        });
//        dfNovelCopyrightFragment.setPageType(HomePageType.COPYRIGHT);
//        dfNovelCopyrightFragment.setViewPager(vpContent);
//        mSweepListViews.add(dfNovelCopyrightFragment);

//
//        cartoonRecommendFragment = new CartoonRecommendFragment();
//        cartoonRecommendFragment.setiListScollerState(this);
//        cartoonRecommendFragment.setOnScrollYListener(new PullToRefreshListView.OnScrollYListener() {
//            @Override
//            public void onScrollY(int scrolledY) {
//                comicY = scrolledY;
//                setTitleBar(scrolledY,HomePageIndex.PAGE_INDEX_COMIC);
//            }
//        });
//        cartoonRecommendFragment.setViewPager(vpContent);
//        mSweepListViews.add(cartoonRecommendFragment);

        CommonViewPagerAdapter viewPagerAdapter = new CommonViewPagerAdapter(getFragmentManager(), mSweepListViews);
        vpContent.setAdapter(viewPagerAdapter);
        vpContent.setOffscreenPageLimit(6);
        tlTop.setupWithViewPager(vpContent);
        vpContent.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                DiaoBaoSharedPreferences.setSharedPreferencesValueToBoolean(SP_KEY_HAS_CHANGE_PAGE,true,WeMediaApplication.applicationContext);
                currentPage = position;
                //统计用户访问频道的数据（包括切换时访问的数据和打开APP时访问的数据）
                HashMap<String, String> map = new HashMap<>();
                map.put("index", String.valueOf(position));
                TMAnalysis.event(getActivity(), UmengEventName.HOME_PAGE_VISIT, map);
                startSpeaker();
                if (isAnalysisLock) {
                    isAnalysisLock = false;
                }else{
                    //统计用户切换频道的数据
                    TMAnalysis.event(getActivity(),UmengEventName.HOME_PAGE_CHANGE,map);
                    if (position == HomePageIndex.PAGE_INDEX_JINGXUAN) {
                        setTitleBar(jingxuanY,position);
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_CHOICE);
                        TMAnalysis.event(getActivity(), "HOME_TOP_BAR_JINGXUAN");
                    }else if (position == HomePageIndex.PAGE_INDEX_ROMANCE) {
                        setTitleBar(romanceY,position);
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_ROMANCE);
                        TMAnalysis.event(getActivity(), "HOME_TOP_BAR_YANQIN");
                    }else if (position == HomePageIndex.PAGE_INDEX_RAINBOW) {
                        setTitleBar(rainbowY,position);
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_RAINBOW);
                        TMAnalysis.event(getActivity(), "HOME_TOP_BAR_CAIHONG");
                    }else if (position == HomePageIndex.PAGE_INDEX_COMIC) {
                        setTitleBar(comicY,position);
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_COMIC);
                        TMAnalysis.event(getActivity(), "HOME_TOP_BAR_CARTOON");
                    } else if (position == HomePageIndex.PAGE_INDEX_RAINBOW_FINISHED) {
                        setTitleBar(rainbowFinishedY,position);
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_RAINBOW_FINISHED);
                        TMAnalysis.event(getActivity(), "HOME_TOP_BAR_CAIHONG_FINISHED");
                    } else if (position == HomePageIndex.PAGE_INDEX_COPYRIGHT) {
                        setTitleBar(copyrightY,position);
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_FIRST_PAGE_COPYRIGHT);
                        TMAnalysis.event(getActivity(), "HOME_TOP_BAR_COPYRIGHT");
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    private void initYouthModeFragments() {
        mSweepListViews = new ArrayList<>();
        if (choiceFragment == null) {
            choiceFragment = new DFChoiceFragment();
            choiceFragment.setiListScollerState(this);
            choiceFragment.setOnScrollYListener(scrolledY -> {
                jingxuanY = scrolledY;
                String pwd = DiaoBaoSharedPreferences.getSharedPreferencesValueToString(SPKey.YOUTH_MODEL_PWD, getActivity(), "");
                if (!(pwd != null && pwd.length() > 0)) {
                    setTitleBar(scrolledY,HomePageIndex.PAGE_INDEX_JINGXUAN);
                }
            });
        }
        choiceFragment.setViewPager(vpContent);
        mSweepListViews.add(choiceFragment);

        CommonViewPagerAdapter viewPagerAdapter = new CommonViewPagerAdapter(getFragmentManager(), mSweepListViews,new String[]{"首页"});
        vpContent.setAdapter(viewPagerAdapter);
        vpContent.setOffscreenPageLimit(6);
        tlTop.setupWithViewPager(vpContent);
    }


    @Override
    public void onResume() {
        super.onResume();
        startSpeaker();
        boolean isNeedUpdate = ((WeMediaApplication) (WeMediaApplication.applicationContext)).isShouldUpdate;
        if (isNeedUpdate) {
            ((WeMediaApplication) (WeMediaApplication.applicationContext)).isShouldUpdate = false;
            if (dfNovelFragment != null) {
                dfNovelFragment.updateNow();
            }
        }
    }



    @Override
    public void onStop() {
        super.onStop();
        stopSpeaker();
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {

    }

    private Colorful mColorful;
    /**
     * 切换主题
     */
    public void changeThemeWithColorful() {
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            mColorful.setTheme(R.style.theme_dark);
        } else {
            mColorful.setTheme(R.style.theme_day);
        }

    }
    private void setupColorful(){
        // 构建Colorful对象来绑定View与属性的对象关系
        mColorful = new Colorful.Builder(getActivity())
                .backgroundColor(R.id.dfhome_rootLayout, R.attr.main_bg)
                // 设置view的背景图片
                .backgroundColor(R.id.layTitleBarBg, R.attr.foreground_bg)
                .create(); // 设置文本颜色
    }
    List<Fragment> fragments = new ArrayList<>();
    public void changeModelBg(){
        if(isNightBg == WeMediaApplication.getInstance().isDayNightMode_Night){
           return;
        }
        isNightBg = !isNightBg;
        fragments.clear();
        for (int i =0;i<mSweepListViews.size();i++){
            if(i == currentPage){
                changeFragmenThemeWithColorful(mSweepListViews.get(i));
            }else {
                fragments.add(mSweepListViews.get(i));
            }
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                for(int i = 0;i<fragments.size();i++){
                    changeFragmenThemeWithColorful(fragments.get(i));
                }
            }
        },100);
    }

    private void changeFragmenThemeWithColorful(Fragment fragment){
        if(fragment == null ){
            return;
        }
        if(fragment instanceof DFBLNovelFragment){
            ((DFBLNovelFragment)fragment).changeThemeWithColorful();
        }else if(fragment instanceof DFGAYFragment){
            ((DFGAYFragment)fragment).changeThemeWithColorful();
        } else if (fragment instanceof CartoonRecommendFragment) {
            ((CartoonRecommendFragment)fragment).changeThemeWithColorful();
        }else if (fragment instanceof DFChoiceFragment) {
            ((DFChoiceFragment)fragment).changeThemeWithColorful();
        }
    }

    @Override
    public void scrollState(int scrollState) {
        switch (scrollState){
            //空闲
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                setAlpha(rlBuoy,1.0f);
                break;
            //滑动
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                setAlpha(rlBuoy,0.3f);
                break;
            //惯性
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                break;
            default:
                break;
        }
    }

    private float currBuoyAlpha = 1.0f;
    /**
     * 设置透明度
     * @param view
     * @param alpha
     */
    private void setAlpha(View view, float alpha) {
        if (view != null) {
            view.setAlpha(alpha);
            ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",currBuoyAlpha,alpha);
            animator.setDuration(200);
            animator.start();
            currBuoyAlpha = alpha;
        }
    }
}
